/*****************************************************************************

File:   otkFamilyTableRowSelectExample.java

Date          Build     Author        Version    Comments
12-Oct-12    P-20-14    dshah         $$1       Created
30-Oct-12    P-20-16    dshah         $$2       Updated
22-Nov-12    P-20-17    dshah         $$3       Updated
20-mar-15 P-30-05 gshmelev $$4  called GetCurrentSession

****************************************************************************/

package com.ptc.otkjavaexamples;
 
import com.ptc.cipjava.*;
import com.ptc.pfc.pfcSession.*;
import com.ptc.pfc.pfcCommand.*;
import com.ptc.pfc.pfcGlobal.*;
import com.ptc.pfc.pfcModel.*;
import com.ptc.pfc.pfcModelItem.*;
import com.ptc.pfc.pfcFeature.*;
import com.ptc.pfc.pfcSolid.*;
import javax.swing.JOptionPane;
import java.io.*;

import com.ptc.cipjava.*;
import com.ptc.pfc.pfcFamily.*;
import com.ptc.pfc.pfcServer.*;
import com.ptc.pfc.pfcWindow.*;
import com.ptc.wfc.wfcSolid.*;
import com.ptc.wfc.wfcFamily.*;
import com.ptc.wfc.wfcModel.*;

 
// Family Table Single Row select
class otkFamilyTableRowSelectExample extends DefaultUICommandActionListener
{



    public otkFamilyTableRowSelectExample()
	{
	}
	
	
	
	
	public void OnCommand()
	{
		addFamilyTableRowSelectExample();
	}
	
	public int addFamilyTableRowSelectExample()
	{

		try
		{
			Session session = pfcSession.GetCurrentSession ();
			WSolid wSolid = (WSolid)(session.GetCurrentModel());
			
			FileWriter file = new FileWriter("FamilyTable_info.txt");
			BufferedWriter infoFile = new BufferedWriter(file);
			
			infoFile.write("=======================");
			infoFile.newLine();
			infoFile.write("-Family Table Example-");
			infoFile.newLine();
			infoFile.write("=======================");
			infoFile.newLine();

			WFamilyMember FamMemb = (WFamilyMember)(wSolid);

			FamilyTableRows FTRows = FamMemb.ListRows();
			int NumberOfRows = FTRows.getarraysize();

			if(NumberOfRows <= 0) // Family table is not found
				return (0);

			// Through UI select 1 for for single row selection and 2 for multiple
			// row selection.

			session.UIDisplayMessage("family_table.txt", "USER Enter index for row selection 1:Single Row, 2:Multiple Row", null);
			Integer SelectedCombStateNo = session.UIReadIntMessage(1, 2);
			if(SelectedCombStateNo.intValue() == 1)
			{
				WFamilyTableRows wFTRows1 = FamMemb.SelectRows(false);
				WFamilyTableRow wFTRow1 = wFTRows1.get(0);
				FamilyTableRow FTRow1 = (FamilyTableRow)(wFTRow1);
        		String InstName1 = FTRow1.GetInstanceName();
				
				infoFile.write("++ Single Row Selected");
				infoFile.newLine();
				infoFile.write("\t++ Instance Name: " + InstName1);
				infoFile.newLine();
				
				boolean flat = wFTRow1.IsFlatState();
				infoFile.write("\t++ Flat State: " +(flat == true ? "TRUE" : "FALSE"));
				infoFile.newLine();

				boolean IsLocked = wFTRow1.GetIsLocked();
				infoFile.write("\t++ Is Locked: " +(IsLocked == true ? "TRUE" : "FALSE"));
				infoFile.newLine();

				WModel ModelfromDisk = wFTRow1.GetModelFromDisk();
				infoFile.write("\t++ Model from Disk Name: " + ModelfromDisk.GetFullName());
				infoFile.newLine();

				boolean is_mod = wFTRow1.IsModifiable(true);
				infoFile.write("\t++ Table Row Modifiable: " +(is_mod == true ? "TRUE" : "FALSE"));
				infoFile.newLine();

				WModel ModelfromSession = wFTRow1.GetModelFromSession();
				infoFile.write("\t++ Model from Session Name: " + ModelfromSession.GetFullName());
				infoFile.newLine();
				infoFile.newLine();

			}

			else if(SelectedCombStateNo.intValue() == 2)
			{
				WFamilyTableRows wFTRows2 = FamMemb.SelectRows(true);			
				int NumberOfRows1 = wFTRows2.getarraysize();
				infoFile.write("++ Multiple Rows Selected");
				infoFile.newLine();
				
				infoFile.write("++ Number of Rows Selected: " + NumberOfRows1);
				infoFile.newLine();

				for(int i=0; i<NumberOfRows1; i++)
				{
					infoFile.write("  ++ Instance: " + (i+1));
					infoFile.newLine();
					
					WFamilyTableRow wFTRow2 = wFTRows2.get(i);
					FamilyTableRow FTRow2 = (FamilyTableRow)(wFTRow2);
					
					String InstName2 = FTRow2.GetInstanceName();
					infoFile.write("\t++ Instance Name: " + InstName2);
					infoFile.newLine();
					
					boolean flat = wFTRow2.IsFlatState();
					infoFile.write("\t++ Flat State: " +(flat == true ? "TRUE" : "FALSE"));
					infoFile.newLine();

					boolean IsLocked = wFTRow2.GetIsLocked();
					infoFile.write("\t++ Is Locked: " +(IsLocked == true ? "TRUE" : "FALSE"));
					infoFile.newLine();

					WModel ModelfromDisk = wFTRow2.GetModelFromDisk();
					infoFile.write("\t++ Model from Disk Name: " + ModelfromDisk.GetFullName());
					infoFile.newLine();

					boolean is_mod = wFTRow2.IsModifiable(true);
					infoFile.write("\t++ Table Row Modifiable: " +(is_mod == true ? "TRUE" : "FALSE"));
					infoFile.newLine();

					WModel ModelfromSession = wFTRow2.GetModelFromSession();
					infoFile.write("\t++ Model from Session Name: " + ModelfromSession.GetFullName());
					infoFile.newLine();
					infoFile.newLine();
				}
			}
			infoFile.close();
		}
		catch (Exception e)
		{
			System.out.println("1: " + e);
		}	
		return 0;
	}
	

};






